const app = getApp();
import { getOrderList, transferOrder, getExpressLis, sendOrder } from '../../utils/requestApi.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.data.imgurl,
    page: 1, //页码
    orderList: [],
    tabbar: ['全部订单', '待处理', '待收货', '已完成'],
    activeItem: 0, // tabbar激活项
    showModal: false, //显示modal
    expressLis: [], //快递公司
    express: '', //快递公司
    expressCode: '', //快递单号
    orderId: '', //订单号
    isSend: false, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    this.initData();

    getExpressLis().then(res =>{
      let { expressLis } = this.data;
      if (res.data.Status == 'Success') {
        res.data.Data.forEach(item =>{
          expressLis.push(item.Name);
        })
        this.setData({ expressLis })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  initData:function(param){
    let { page, orderList, activeItem } = this.data;
    wx.showLoading({ title: '加载中...' })
    if (param) {
      orderList = []; page = 1
    }
    getOrderList({
      IsReferralUserId: true,
      pageSize: 10,
      pageIndex: page,
      status: activeItem == 2 ? 3 : (activeItem == 3 ? 5 : activeItem)
    }).then(res =>{
      wx.hideLoading();
      if (res.data.Data.Status = 'Success' && res.data.Data.length !=0){
        orderList = [...orderList, ...res.data.Data]; 
        page ++ ;
      }
      this.setData({ orderList, page })
    })
  },

  // 选择tabbar
  selectTabbar: function (e) {
    this.setData({
      activeItem: e.detail.index,
      page: 1
    })
    this.initData(true);
  },

  // 调货
  onTransfer(e){
    let _this = this;
    wx.showModal({
      content: '确定调货？',
      success(res) {
        if (res.confirm) {
          transferOrder({ orderId: e.currentTarget.dataset.id }).then(res => {
            if (res.data.Status == 'Success') {
              setTimeout(() => _this.initData(true), 1000)
            }
            wx.showToast({ icon: 'none', title: res.data.Message })
          })
        } else if (res.cancel) console.log('用户点击取消')
      }
    })
  },
 
  onExpress(e){
    let { expressLis } = this.data;
    if (e.currentTarget.dataset.id == 0) this.setData({ express: expressLis[e.detail.value] })
    else this.setData({ expressCode: e.detail.value })
  },

  //关闭弹出层
  onClose(e){
    let { id } = e.currentTarget.dataset
    if (id) this.setData({ showModal: !this.data.showModal, orderId: id })
    else this.setData({ showModal: !this.data.showModal, isSend: false, express: '', expressCode: ''})
  },

  // 发货
  onSend(e){
    let { express, expressCode, orderId } = this.data;
    if (!express) {
      wx.showToast({ icon: 'none', title: '请选择快递公司' })
      return
    }
    if (!expressCode) {
      wx.showToast({ icon: 'none', title: '请填写快递单号' })
      return
    }
    this.setData({ isSend: true })
    sendOrder({
      OrderId: orderId,
      CompanyName: express,
      BillID: expressCode
    }).then(res =>{
      this.setData({ isSend: false, showModal: false, express: '', expressCode: '' })
      if (res.data.Status == 'Success') {
        setTimeout(() => this.initData(true),1000)
      }
      wx.showToast({ icon: 'none', title: res.data.Message })
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.initData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})